package com.chixing.service.impl;

import com.chixing.mapper.NotesCollectMapper;
import com.chixing.mapper.SightsCollectMapper;
import com.chixing.pojo.NotesCollect;
import com.chixing.pojo.NotesCollectExample;
import com.chixing.pojo.SightsCollect;
import com.chixing.pojo.SightsCollectExample;
import com.chixing.service.CollectionService;
import com.chixing.util.DateUtil;
import com.chixing.util.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private SightsCollectMapper sightsCollectMapper;
    @Autowired
    private NotesCollectMapper notesCollectMapper;

    @Override
    public ServerResponse isCollectionByCustId(Integer custId) {
        SightsCollectExample sightsCollectExample = new SightsCollectExample();
        sightsCollectExample.createCriteria().andCustIdEqualTo(custId);
        List<Integer> sightsIds = new ArrayList<>();
        List<SightsCollect> sightsCollects = sightsCollectMapper.selectByExample(sightsCollectExample);
        for (SightsCollect s:sightsCollects
             ) {
            sightsIds.add(s.getTravelSightsId());
            log.info("该用户收藏了"+s.getTravelSightsId());
        }
        if (sightsCollects.size()>0){
            return ServerResponse.getSuccess(sightsIds);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse addCollection(Integer custId, Integer sightsId) {
        SightsCollect sightsCollect = new SightsCollect();
        sightsCollect.setCustId(custId);
        sightsCollect.setTravelSightsId(sightsId);
        String dateStr = DateUtil.date2String(new Date(),DateUtil.DATE_PATTERN_YMDHMS);
        Date date = DateUtil.string2Date(dateStr,DateUtil.DATE_PATTERN_YMDHMS);
        sightsCollect.setSightsCollectTime(date);
        int insert = sightsCollectMapper.insert(sightsCollect);
        if (insert>0){
            return ServerResponse.getSuccess(insert);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse isNotesCollectionByCustId(Integer custId) {
        NotesCollectExample notesCollectExample = new NotesCollectExample();
        notesCollectExample.createCriteria().andCustIdEqualTo(custId);
        List<Integer> notesIds = new ArrayList<>();

        List<NotesCollect> notesCollects = notesCollectMapper.selectByExample(notesCollectExample);
        for (NotesCollect n:notesCollects
        ) {
            notesIds.add(n.getTravelNotesId());
            System.out.println("该用户收藏了"+n.getTravelNotesId());
        }
        if (notesCollects.size()>0){
            return ServerResponse.getSuccess(notesIds);
        }
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse addNotesCollection(Integer custId, Integer notesId) {
        NotesCollect notesCollect = new NotesCollect();
        notesCollect.setCustId(custId);
        notesCollect.setTravelNotesId(notesId);
        String dateStr = DateUtil.date2String(new Date(),DateUtil.DATE_PATTERN_YMDHMS);
        Date date = DateUtil.string2Date(dateStr,DateUtil.DATE_PATTERN_YMDHMS);
        notesCollect.setNotesCollectTime(date);
        int insert = notesCollectMapper.insert(notesCollect);
        if (insert>0){
            return ServerResponse.getSuccess(insert);
        }
        return ServerResponse.getFail(null);
    }
}
